package com.lei.study.utils;

import com.alibaba.fastjson.JSON;

import java.nio.charset.StandardCharsets;

/**
 * <p>
 * JsonUtils
 * </p>
 *
 * @author <a href="mailto:17674030991@163.com">伍磊</a>
 */
public class JsonUtils {

    /**
     * 序列化
     *
     * @param obj
     * @return
     */
    public static byte[] serialize(Object obj) {
        return JSON.toJSONString(obj).getBytes(StandardCharsets.UTF_8);
    }

    /**
     * 反序列化
     *
     * @param data
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T deserialize(byte[] data, Class<T> clazz) {
        return JSON.parseObject(new String(data, StandardCharsets.UTF_8), clazz);
    }
}